﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using Abp.UI;
using CPPProject.CPP.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace CPPProject.CPP
{
    public class CPPCompanyAppService: AsyncCrudAppService<CPPCompany, CPPCompanyDto,long, PagedResultRequestDto, CreateCPPCompanyDto, UpdateCPPCompanyDto>, ICPPCompanyAppService
    {
        private readonly IObjectMapper _objectMapper;
        public CPPCompanyAppService(
            IRepository<CPPCompany, long> repository,
            IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
        }
        /// <summary>
        /// 查询公司（分页模糊）
        /// </summary>
        /// <param name="skipCount">跳过条数</param>
        /// <param name="maxResultCount">单次最大查询数量</param>
        /// <param name="name">公司名称</param>
        /// <returns></returns>
        public async Task<PagedResultDto<CPPCompanyDto>> GetList(int skipCount = 0, int maxResultCount = 10, string name = "")
        {
            PagedResultDto<CPPCompanyDto> resultDto = new PagedResultDto<CPPCompanyDto>();
            List<CPPCompany> lists = new List<CPPCompany>();
            Expression<Func<CPPCompany, bool>> expr = null;
            if (!string.IsNullOrEmpty(name))
            {
                expr = expr == null ? o => o.Name.Contains(name) : expr.ExpressionAnd(o => o.Name.Contains(name));
            }
            if (expr != null)
            {
                lists = await Repository.GetAllListAsync(expr);
                var dtos = lists.Skip(skipCount).Take(maxResultCount).ToList();
                resultDto.Items = _objectMapper.Map<List<CPPCompanyDto>>(dtos);
                resultDto.TotalCount = lists.Count;
            }
            else
            {
                resultDto = await base.GetAllAsync(new PagedResultRequestDto { MaxResultCount = maxResultCount, SkipCount = skipCount });
            }
            return resultDto;
        }

        /// <summary>
        /// 重写增加方法，判断公司名称是否重复
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<CPPCompanyDto> CreateAsync(CreateCPPCompanyDto input)
        {
            var lists = await Repository.GetAllListAsync(o => o.Name == input.Name);
            if (lists.Count <= 0)
            {
                return await base.CreateAsync(input);
            }
            else
            {
                throw new UserFriendlyException(input.Name + "已经存在");
            }
        }


        /// <summary>
        /// 公司下拉，要根据当前登录用户权限获取公司的ID
        /// </summary>
        /// <returns></returns>
        public async Task<List<GetDoawRowDto>> GetDoawRow()
        {
            List<CPPCompany> lists = new List<CPPCompany>();
            List<GetDoawRowDto> list = new List<GetDoawRowDto>();
            lists = await Repository.GetAllListAsync();
            for (int i = 0; i < lists.Count; i++)
            {
                GetDoawRowDto getDoawRow = new GetDoawRowDto();
                getDoawRow.value = lists[i].Id.ToString();
                getDoawRow.label = lists[i].Name;
                list.Add(getDoawRow);
            }
            return list;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">id</param>
        /// <returns></returns>
        public async Task BatchDelete(long[] ids)
        {
            if (ids != null && ids.Count() > 0)
            {
                await Repository.DeleteAsync(it => ids.Contains(it.Id));
            }
        }


    }
}
